package com.cjkj.hospital.service.impl;

import com.cjkj.common.constant.HttpStatus;
import com.cjkj.common.exception.ServiceException;
import com.cjkj.common.utils.DateUtils;
import com.cjkj.common.utils.uuid.IdUtils;
import com.cjkj.hospital.domain.THospitalDept;
import com.cjkj.hospital.domain.THospitalInfo;
import com.cjkj.hospital.domain.THospitalUser;
import com.cjkj.hospital.domain.dto.HospitalDeptReq;
import com.cjkj.hospital.domain.vo.HospitalDeptVo;
import com.cjkj.hospital.mapper.THospitalDeptMapper;
import com.cjkj.hospital.mapper.THospitalInfoMapper;
import com.cjkj.hospital.mapper.THospitalUserMapper;
import com.cjkj.hospital.service.ITHospitalDeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.cjkj.common.utils.SecurityUtils.getUsername;

/**
 * 医院科室信息Service业务层处理
 * 
 * @author cjkj
 * @date 2024-08-28
 */
@Service
public class THospitalDeptServiceImpl implements ITHospitalDeptService 
{
    @Resource
    private THospitalInfoMapper tHospitalInfoMapper;
    @Resource
    private THospitalDeptMapper tHospitalDeptMapper;
    @Resource
    private THospitalUserMapper tHospitalUserMapper;

    /**
     * 查询医院科室信息
     * 
     * @param deptId 医院科室信息主键
     * @return 医院科室信息
     */
    @Override
    public THospitalDept selectTHospitalDeptByDeptId(String deptId)
    {
        return tHospitalDeptMapper.selectTHospitalDeptByDeptId(deptId);
    }

    /**
     * 查询医院科室信息列表
     * 
     * @param req 医院科室信息
     * @return 医院科室信息
     */
    @Override
    public List<HospitalDeptVo> selectTHospitalDeptList(HospitalDeptReq req)
    {
        return tHospitalDeptMapper.selectTHospitalDeptList(req);
    }

    /**
     * 新增医院科室信息
     * 
     * @param tHospitalDept 医院科室信息
     * @return 结果
     */
    @Override
    public int insertTHospitalDept(THospitalDept tHospitalDept)
    {
        String hospitalId = tHospitalDept.getHospitalId();
        String deptName = tHospitalDept.getDeptName();
        THospitalDept baseInfo = tHospitalDeptMapper.selectByHosIdAndName(hospitalId, deptName);
        if(baseInfo != null) {
            THospitalInfo tHospitalInfo = tHospitalInfoMapper.selectTHospitalInfoByHospitalId(hospitalId);
            throw new ServiceException(tHospitalInfo.getHospitalName() + "已存在名为" + deptName + "的科室！");
        }

        tHospitalDept.setDeptId(IdUtils.fastSimpleUUID());
        Date nowDate = DateUtils.getNowDate();
        String username = getUsername();
        tHospitalDept.setCreateBy(username);
        tHospitalDept.setCreateTime(nowDate);
        tHospitalDept.setUpdateBy(username);
        tHospitalDept.setUpdateTime(nowDate);
        return tHospitalDeptMapper.insertTHospitalDept(tHospitalDept);
    }

    /**
     * 修改医院科室信息
     * 
     * @param tHospitalDept 医院科室信息
     * @return 结果
     */
    @Override
    public int updateTHospitalDept(THospitalDept tHospitalDept)
    {
        String hospitalId = tHospitalDept.getHospitalId();
        String deptName = tHospitalDept.getDeptName();
        THospitalDept baseInfo = tHospitalDeptMapper.selectByHosIdAndName(hospitalId, deptName);

        if(baseInfo != null && ! baseInfo.getDeptId().equals(tHospitalDept.getDeptId())) {
            THospitalInfo tHospitalInfo = tHospitalInfoMapper.selectTHospitalInfoByHospitalId(hospitalId);
            throw new ServiceException(tHospitalInfo.getHospitalName() + "已存在名为" + deptName + "的科室！");
        }

        tHospitalDept.setUpdateBy(getUsername());
        tHospitalDept.setUpdateTime(DateUtils.getNowDate());
        return tHospitalDeptMapper.updateTHospitalDept(tHospitalDept);
    }

    /**
     * 批量删除医院科室信息
     * 
     * @param deptIds 需要删除的医院科室信息主键
     * @return 结果
     */
    @Override
    public String deleteTHospitalDeptByDeptIds(String[] deptIds)
    {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (String deptId : deptIds) {
            // 判断科室是否还有未删除的账号

            // 1、查询科室信息
            THospitalDept tHospitalDept = tHospitalDeptMapper.selectTHospitalDeptByDeptId(deptId);
            if(tHospitalDept == null) {
                failureNum ++;
                failureMsg.append("<br/>").append(failureNum).append("、科室ID ").append(deptId).append(" 不存在！");
                continue;
            }
            String deptName = tHospitalDept.getDeptName();

            // 2、查询是否科室下的账号
            List<THospitalUser> tHospitalUsers = tHospitalUserMapper.selectUsersByDeptId(deptId);
            if(!tHospitalUsers.isEmpty()) {
                failureNum ++;
                failureMsg.append("<br/>").append(failureNum).append("、科室 ").append(deptName).append(" 有未删除的账号信息！");
                continue;
            }

            // 3、删除科室
            int i = tHospitalDeptMapper.deleteTHospitalDeptByDeptId(deptId);
            if (i > 0) {
                successNum ++;
                successMsg.append("<br/>" + successNum + "、科室 " + deptName + " 删除成功！");
            } else {
                failureNum ++;
                failureMsg.append("<br/>" + failureNum + "、科室 " + deptName + " 删除失败！");
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，共 " + failureNum + " 条数据删除失败，错误如下：");
            throw new ServiceException(failureMsg.toString(), HttpStatus.ERROR_PLUS);
        } else {
            successMsg.insert(0, "恭喜您，数据已全部删除成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<Map<String, String>> getDeptOptionData(String hospitalId) {
        return tHospitalDeptMapper.selectDeptOptionData(hospitalId);
    }
}
